<?php
if ( function_exists('register_sidebar') )
register_sidebar();

/* next is init config panel fields */
$themename = "A16006-A";
$shortname = "a16006a";
$options = array (
array("name" => "Welcome Message",
        "type" => "title"),
array("type" => "open"),
//--- ok start fields ---
array(    "name" => "Service Page",
        "desc" => "Enter the id for Service Page.",
        "id" => $shortname."_service_page",
        "std" => "3",
        "type" => "text"),
array(    "name" => "Service Excerpt",
        "desc" => "Enter some short sentence descript your Services or Products. If left empty ,it will use Service page conetnt",
        "id" => $shortname."_service_expcert",
        "std" => "ff",
        "type" => "textarea"),
array(    "name" => "Poster Tags",
        "desc" => "When a post's tags meet one of these tags, this post's image will show in home page.",
        "id" => $shortname."_poster_tags",
        "std" => "poster",
        "type" => "text"),
array(    "type" => "close")
);

/**
 * WordPress standard function , initializ theme option panel.
 * @return unknown_type
 */
function mytheme_add_admin(){
	global $themename, $shortname, $options;

	if ( $_GET['page'] == basename(__FILE__) ) {
		if ( 'save' == $_REQUEST['action'] ) {
			foreach ($options as $value) {
				update_option( $value['id'], $_REQUEST[ $value['id'] ] );
			}

			foreach ($options as $value) {
				if( isset( $_REQUEST[ $value['id'] ] ) ) {
					update_option( $value['id'], $_REQUEST[ $value['id'] ]  );
				} else {
					delete_option( $value['id'] );
				}
			}
			header("Location: themes.php?page=functions.php&saved=true");
			die;
		} else if( 'reset' == $_REQUEST['action'] ) {
			foreach ($options as $value) {
				delete_option( $value['id'] );
			}
			header("Location: themes.php?page=functions.php&reset=true");
			die;

		}
	}

	add_theme_page($themename." Options", "".$themename." Options", 'edit_themes', basename(__FILE__), 'mytheme_admin');

}

/**
 * WordPress standar function
 * @return unknown_type
 */
function mytheme_admin() {

	global $themename, $shortname, $options;

	if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved.</strong></p></div>';
	if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>';

	?>
<div class="wrap">
<h2><?php echo $themename; ?> Settings</h2>

<form method="post"><?php foreach ($options as $value) {
	switch ( $value['type'] ) {

		case "open":
			?>
<table width="100%" border="0"
	style="background-color: #eef5fb; padding: 10px;">

	<?php break;

case "close":
	?>

</table>
<br />

	<?php break;

case "title":
	?>
<table width="100%" border="0"
	style="background-color: #dceefc; padding: 5px 10px;">
	<tr>
		<td colspan="2">
		<h3 style="font-family: Georgia, 'Times New Roman', Times, serif;"><?php echo $value['name']; ?></h3>
		</td>
	</tr>

	<?php break;

case 'text':
	?>

	<tr>
		<td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
		<td width="80%"><input style="width: 400px;"
			name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>"
			type="<?php echo $value['type']; ?>"
			value="<?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>" /></td>
	</tr>

	<tr>
		<td><small><?php echo $value['desc']; ?></small></td>
	</tr>
	<tr>
		<td colspan="2"
			style="margin-bottom: 5px; border-bottom: 1px dotted #000000;">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>

	<?php
	break;

case 'textarea':
	?>

	<tr>
		<td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
		<td width="80%"><textarea name="<?php echo $value['id']; ?>"
			style="width: 400px; height: 200px;"
			type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?></textarea></td>

	</tr>

	<tr>
		<td><small><?php echo $value['desc']; ?></small></td>
	</tr>
	<tr>
		<td colspan="2"
			style="margin-bottom: 5px; border-bottom: 1px dotted #000000;">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>

	<?php
	break;

case 'select':
	?>
	<tr>
		<td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
		<td width="80%"><select style="width: 240px;"
			name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
			<?php foreach ($value['options'] as $option) { ?>
			<option
			<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option>
			<?php } ?>
		</select></td>
	</tr>

	<tr>
		<td><small><?php echo $value['desc']; ?></small></td>
	</tr>
	<tr>
		<td colspan="2"
			style="margin-bottom: 5px; border-bottom: 1px dotted #000000;">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>

	<?php
	break;

case "checkbox":
	?>
	<tr>
		<td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
		<td width="80%"><?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>
		<input type="checkbox" name="<?php echo $value['id']; ?>"
			id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
		</td>
	</tr>

	<tr>
		<td><small><?php echo $value['desc']; ?></small></td>
	</tr>
	<tr>
		<td colspan="2"
			style="margin-bottom: 5px; border-bottom: 1px dotted #000000;">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>

	<?php break;

	}
}
?>

	<p class="submit"><input name="save" type="submit" value="Save changes" />
	<input type="hidden" name="action" value="save" /></p>
	</form>
	<form method="post">
	<p class="submit"><input name="reset" type="submit" value="Reset" /> <input
		type="hidden" name="action" value="reset" /></p>
	</form>

	<?php
}

// register theme's option panel to admin control panel.
add_action('admin_menu', 'mytheme_add_admin');

/**
 * initialization theme option's variables.
 * if needed ,put this function in the head of index.php,sidebar.php,single.php or other.
 * @return unknown_type
 */
function initThemeVariables(){
	global $options;
	foreach ($options as $value) {
		if (get_settings( $value['id'] ) === FALSE) {
			global $$value['id'];
			$$value['id'] = $value['std'];
		} else {
			global $$value['id'];
			$$value['id'] = get_settings( $value['id'] );
		}
	}

}

/**
 * Register initThemeVariables to the WordPress head.
 * When a page started , initializ those theme variables first.
 */
add_action('wp_head','initThemeVariables');

/**
 * Get the newest poster article.
 * @param $number
 * @return unknown_type
 */
function lastPoster($number = 1){
	global $a16006a_poster_tags;
	if($a16006a_poster_tags===FALSE){
		$a16006a_poster_tags="poster";
	}
	$args = array(
	'numberposts' => $number,
	'tag' => $a16006a_poster_tags,
	'orderby'=>"date"
	);
	$rand_posts = get_posts($args);
	return $rand_posts;
}

/**
 * Check if an article is set as 'poster'.
 * @param $posttags The article object for checked.
 * @return return true , if the article was set as 'poster'. Else return false ;
 */
function isPoster($posttags){
	global $a16006a_poster_tags;
	if($a16006a_poster_tags===FALSE){
		$a16006a_poster_tags="poster";
	}
	$tags = preg_split("[,;.]",$a16006a_poster_tags,-1,PREG_SPLIT_NO_EMPTY);
	if ($posttags) {
		foreach($posttags as $tag) {
			foreach($tags as $poster){
				if($tag->name == $poster) return true;
			}
		}
	}
	return false;
}



/* (thumbnail, medium, large or full) */
function the_image($size = 'medium' , $class = ''){
	global $post;
	//setup the attachment array
	$att_array = array(
		'numberposts' => 1,
		'post_parent' =>$post->ID,
		'post_type' =>'attachment',
		'post_mime_type'=> 'image',
		'order_by'=>'menu_order',
		'order'=>'ASC'
		);

		//get the post attachments
		$attachments = get_children($att_array);

		//make sure there are attachments
		if (is_array($attachments)){

			//loop through them
			foreach($attachments as $att){
				//find the one we want based on its characteristics
				if ( $att->menu_order == 0){
					$image_src_array = wp_get_attachment_image_src($att->ID, $size);

					//get url - 1 and 2 are the x and y dimensions
					$url = $image_src_array[0];
					$caption = $att->post_excerpt;
					$image_html = '<img class="%s" src="%s" alt="%s" />';
					//combine the data
					$html = sprintf($image_html,$class,$url,$caption);
					//echo the result
					echo $html;
				}
			}
		}

}

function get_ServicesPages(){
	global $a16006a_service_page;
	if($a16006a_service_page===FALSE){
		$a16006a_service_page="poster";
	}
	$args = array(
    'child_of' => $a16006a_service_page,
    'sort_order' => 'ASC',
    'sort_column' => 'post_title',
    'hierarchical' => 1); 
	return get_pages($args);
}

?>